package cn.com.nes.common.web;

import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.util.Assert;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

@ApiModel(value = "统一返回对象模型")
@Data
public class CommonResult<T> implements Serializable {

    /**
     * 状态码
     */
    @ApiModelProperty(value = "状态码")
    private Integer retCode = RetCodeConstants.ERROR;

    /**
     * 返回数据
     */
    @ApiModelProperty(value = "返回数据")
    private T data;

    /**
     * 错误提示，用户可阅读
     */
    @ApiModelProperty(value = "错误提示")
    private String retMessage;

    public static <T> CommonResult<T> error(Integer retCode, String retMessage) {
        Assert.isTrue(!RetCodeConstants.SUCCESS.equals(retCode), "code 必须是错误的！");
        CommonResult<T> result = new CommonResult<>();
        result.setRetCode(retCode);
        result.setRetMessage(retMessage);
        return result;
    }

    public static <T> CommonResult<T> error(String retMessage) {
        CommonResult<T> result = new CommonResult<>();
        result.setRetCode(RetCodeConstants.ERROR);
        result.setRetMessage(retMessage);
        return result;
    }


    public static <T> CommonResult<T> ok() {
        CommonResult<T> result = new CommonResult<>();
        result.setRetCode(0);
        result.setRetMessage("OK");
        return result;
    }

    public static <T> CommonResult<T> ok(String retMessage) {
        CommonResult<T> result = new CommonResult<>();
        result.setRetCode(RetCodeConstants.SUCCESS);
        result.setRetMessage(retMessage);
        return result;
    }

    public static CommonResult<Map<String, Object>> data(String key, Object data) {
        CommonResult<Map<String, Object>> result = new CommonResult<>();
        result.setData(MapUtil.builder(key, data).build());
        result.setRetCode(RetCodeConstants.SUCCESS);
        result.setRetMessage("");
        return result;
    }

    public static <T> CommonResult<T> data(T data) {
        CommonResult<T> result = new CommonResult<>();
        result.setData(data);
        result.setRetCode(RetCodeConstants.SUCCESS);
        result.setRetMessage("");
        return result;
    }

    /* 分页 */

    @ApiModelProperty(value = "页数")
    private Long pagecount;

    @ApiModelProperty(value = "每页条数")
    private Long pagesize;

    @ApiModelProperty(value = "页码")
    private Long pageid;

    @ApiModelProperty(value = "总条数")
    private Long recordcount;

    @ApiModelProperty(value = "分页数据")
    private List<T> page;

    public static <T> CommonResult<T> page(List<T> list) {
        CommonResult<T> result = new CommonResult<>();
        result.setRetCode(0);
        result.setRetMessage("");
        result.setPage(list);
        result.setPageid(1L);
        result.setPagesize(Long.parseLong(list.size() + ""));
        result.setPagecount(1L);
        result.setRecordcount(Long.parseLong(list.size() + ""));
        return result;
    }

    public static <T> CommonResult<T> page(IPage<T> page) {
        CommonResult<T> result = new CommonResult<>();
        result.setRetCode(0);
        result.setRetMessage("");
        result.setPage(page.getRecords());
        result.setPageid(page.getCurrent());
        result.setPagesize(page.getSize());
        result.setPagecount(page.getPages());
        result.setRecordcount(page.getTotal());
        return result;
    }


}
